package com.zhong.gang.app.ui.contract;

import android.content.Intent;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.zhong.gang.app.R;
import com.zhong.gang.app.api.Api;
import com.zhong.gang.app.api.HttpUtils;
import com.zhong.gang.app.db.UserUtils;
import com.zhong.gang.applib.base.act.ActBaseRefresh;
import com.zhong.gang.applib.config.Config;
import com.zhong.gang.applib.databinding.LayoutPullToBinding;
import com.zhong.gang.applib.http.BaseDataBean;
import com.zhong.gang.applib.modle.ListBean;
import java.util.ArrayList;
import java.util.List;

public class ContractAct extends ActBaseRefresh<LayoutPullToBinding,ContractAdapter> implements BaseQuickAdapter.OnItemClickListener {
    @Override
    public int LayoutRes() {
        return R.layout.layout_pull_to;
    }

    @NonNull
    @Override
    public String CreateTitle() {
        return "请选择";
    }

    @Override
    public void initView() {
        bind.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        initrefrsh(bind.pullto,new ContractAdapter(),bind.recyclerView);
        adapter.setOnItemClickListener(this);
        getData();
    }

    @Override
    public void getData() {
        super.getData();
        HttpUtils.post(Api.getContractList,this)
                .addParams("current",page+"")
                .addParams("size","20")
                .addParams("steelMillId", UserUtils.getIntent(this).getUser().getSteelMillId()+"")
                .build().execute(new ActCallBack<BaseDataBean<ListBean<ContractBean>>>(){
            @Override
            public void onResponse(BaseDataBean<ListBean<ContractBean>> data) throws Exception {
                super.onResponse(data);
                List<ContractBean.ContractMaterialTypesBean> dataList = new ArrayList<>();
                if (data.getResult() != null && data.getResult().getRecords() != null){
                    for (ContractBean item : data.getResult().getRecords()){
                        for (ContractBean.ContractMaterialTypesBean bean : item.getContractMaterialTypes()) {
                            bean.setId(item.getId());
                            dataList.add(bean);
                        }
                    }
                }
                setData(dataList);
            }

            @Override
            public void onError(String str) {
                super.onError(str);
                setData(new ArrayList<ContractBean.ContractMaterialTypesBean>());
            }
        });
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        ContractBean.ContractMaterialTypesBean bean = (ContractBean.ContractMaterialTypesBean) adapter.getItem(position);
        Intent intent = new Intent();
        intent.putExtra(Config.CONTRACT,bean);
        setResult(RESULT_OK,intent);
        closeActivity();
    }
}
